package com.lanlian.cayman.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.LLApplication;
import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.RegisterResultActivity;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.SelectSheetDialog;
import com.lanlian.cayman.view.ShowAgreementDialog;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.PreRegisterExtendInfo;
import com.lanlian.commonlib.bean.RegisterEnumInfo;
import com.lanlian.commonlib.bean.ReqTips;
import com.lanlian.commonlib.bean.RespTips;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.commonlib.util.OnEventID;
import com.lanlian.commonlib.util.OnEventUtils;
import com.lanlian.utillib.CommonUtils;
import com.lanlian.utillib.WorksSizeCheckUtil;

import java.util.ArrayList;

import static com.lanlian.cayman.activity.OpenAccentActivity.respPreRegister;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 14:18
 *     desc   : 注册开户第三步，账户信息
 *     version: 1.0
 * </pre>
 */
@SuppressLint("ValidFragment")
public class RegisterFourFragment extends BaseFragment implements View.OnClickListener {

    private LinearLayout mLayOccupationType;
    private TextView mTvOccupationType;
    private EditText mEtCompanyName;
    private LinearLayout mLayCompanyType;
    private TextView mTvCompanyType;
    private EditText mEtCompanyAddress;
    private TextView mTvSubmit;
    private TextView mTvPrevious;
    private LinearLayout mLayPosition;
    private TextView mTvPosition;
    private EditText mEtYear;

    private boolean mIsEditHasContent;
    private boolean mIsFirst = true;

    private String mOccupationStr;
    private String mCompanyTypeStr;
    private String mPositionStr;

    private OnPreviousClickListener mOnPreviousClickListener;

    private SelectSheetDialog dialog1;
    private SelectSheetDialog dialog2;
    private SelectSheetDialog dialog3;

    public interface OnPreviousClickListener {
        void onPreviousClick();
    }

    public RegisterFourFragment(OnPreviousClickListener onPreviousClickListener) {
        this.mOnPreviousClickListener = onPreviousClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register_four, null);
        initView(view);
        setListeners();
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLayOccupationType = view.findViewById(R.id.lay_occupation_type);
        mTvOccupationType = view.findViewById(R.id.tv_occupation_type);
        mEtCompanyName = view.findViewById(R.id.et_company_name);
        mLayCompanyType = view.findViewById(R.id.lay_company_type);
        mTvCompanyType = view.findViewById(R.id.tv_company_type);
        mEtCompanyAddress = view.findViewById(R.id.et_company_address);
        mTvSubmit = view.findViewById(R.id.tv_submit);
        mTvPrevious = view.findViewById(R.id.tv_previous);
        mLayPosition = view.findViewById(R.id.lay_position);
        mTvPosition = view.findViewById(R.id.tv_position);
        mEtYear = view.findViewById(R.id.et_year);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvSubmit.setOnClickListener(this);
        mTvPrevious.setOnClickListener(this);
        mLayOccupationType.setOnClickListener(this);
        mLayCompanyType.setOnClickListener(this);
        mLayPosition.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvSubmit);
        worksSizeCheckUtil.addAllEditText(mTvCompanyType, mTvOccupationType, mEtCompanyAddress,
                mEtCompanyName, mTvPosition, mEtYear);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if ("职员".equals(mTvOccupationType.getText().toString()) ||
                        "雇主".equals(mTvOccupationType.getText().toString())) {
                    mIsEditHasContent = isHasContent;
                } else {
                    mIsEditHasContent = !TextUtils.isEmpty(mTvOccupationType.getText().toString());
                }
                LLApplication.reqUserRegister.setCompanyName(mEtCompanyName.getText().toString());
                LLApplication.reqUserRegister.setCompanyAddress(mEtCompanyAddress.getText().toString());
                LLApplication.reqUserRegister.setWorkingLife(mEtYear.getText().toString());
                checkData();
            }
        });
    }

    private void initDialog() {
        dialog1 = new SelectSheetDialog(getActivity(), "职业类型",
                AbsListView.CHOICE_MODE_SINGLE, RegisterThreeFragment.registerEnumMap.get("JobType"));
        dialog1.setOnFinishClickListener(new SelectSheetDialog.OnFinishClickListener() {
            @Override
            public void onFinishClick(ArrayList<RegisterEnumInfo> strings) {
                if (!CommonUtils.isListEmpty(strings)) {
                    mTvOccupationType.setText(strings.get(0).getContent());
                    mOccupationStr = strings.get(0).getID();
                    LLApplication.reqUserRegister.setCareerType(mOccupationStr);
                }
            }
        });
        dialog2 = new SelectSheetDialog(getActivity(), "公司类型",
                AbsListView.CHOICE_MODE_SINGLE, RegisterThreeFragment.registerEnumMap.get("CompanyType"));
        dialog2.setOnFinishClickListener(new SelectSheetDialog.OnFinishClickListener() {
            @Override
            public void onFinishClick(ArrayList<RegisterEnumInfo> strings) {
                if (!CommonUtils.isListEmpty(strings)) {
                    mTvCompanyType.setText(strings.get(0).getContent());
                    mCompanyTypeStr = strings.get(0).getID();
                    LLApplication.reqUserRegister.setCompanyType(mCompanyTypeStr);
                }
            }
        });
        dialog3 = new SelectSheetDialog(getActivity(), "公司职位",
                AbsListView.CHOICE_MODE_SINGLE, RegisterThreeFragment.registerEnumMap.get("Position"));
        dialog3.setOnFinishClickListener(new SelectSheetDialog.OnFinishClickListener() {
            @Override
            public void onFinishClick(ArrayList<RegisterEnumInfo> strings) {
                if (!CommonUtils.isListEmpty(strings)) {
                    mTvPosition.setText(strings.get(0).getContent());
                    mPositionStr = strings.get(0).getID();
                    LLApplication.reqUserRegister.setUserPosition(mPositionStr);
                }
            }
        });
    }

    public void setPreRegisterInfo() {
        if (respPreRegister != null && mIsFirst && RegisterThreeFragment.registerEnumMap != null) {
            mIsFirst = false;
            initDialog();
            if (respPreRegister.getUserExtendInfo() == null) {
                return;
            }
            PreRegisterExtendInfo registerExtendInfo = respPreRegister.getUserExtendInfo();
            mOccupationStr = registerExtendInfo.getCareerType();
            for (RegisterEnumInfo assetSource : RegisterThreeFragment.registerEnumMap.get("JobType")) {
                if (assetSource.getID().equals(mOccupationStr)) {
                    mTvOccupationType.setText(assetSource.getContent());
                    dialog1.setSingleChecked(RegisterThreeFragment.registerEnumMap.get("JobType").indexOf(assetSource));
                    break;
                }
            }
            mEtCompanyName.setText(registerExtendInfo.getCompanyName());
            mCompanyTypeStr = registerExtendInfo.getCompanyType();
            for (RegisterEnumInfo assetSource : RegisterThreeFragment.registerEnumMap.get("CompanyType")) {
                if (assetSource.getID().equals(mCompanyTypeStr)) {
                    mTvCompanyType.setText(assetSource.getContent());
                    dialog2.setSingleChecked(RegisterThreeFragment.registerEnumMap.get("CompanyType").indexOf(assetSource));
                    break;
                }
            }
            mEtCompanyAddress.setText(registerExtendInfo.getCompanyAddress());
            mPositionStr = registerExtendInfo.getUserPosition();
            for (RegisterEnumInfo assetSource : RegisterThreeFragment.registerEnumMap.get("Position")) {
                if (assetSource.getID().equals(mPositionStr)) {
                    mTvPosition.setText(assetSource.getContent());
                    dialog3.setSingleChecked(RegisterThreeFragment.registerEnumMap.get("Position").indexOf(assetSource));
                    break;
                }
            }
            mEtYear.setText(registerExtendInfo.getWorkingLife());

            LLApplication.reqUserRegister.setCareerType(mOccupationStr);
            LLApplication.reqUserRegister.setCompanyType(mCompanyTypeStr);
            LLApplication.reqUserRegister.setUserPosition(mPositionStr);
            LLApplication.reqUserRegister.setCompanyName(registerExtendInfo.getCompanyName());
            LLApplication.reqUserRegister.setCompanyAddress(registerExtendInfo.getCompanyAddress());
            LLApplication.reqUserRegister.setWorkingLife(registerExtendInfo.getWorkingLife());
        }
    }

    private void checkData() {
        if (mIsEditHasContent) {
            mTvSubmit.setEnabled(true);
            mTvSubmit.setBackgroundResource(R.mipmap.login_btn);
        } else {
            mTvSubmit.setEnabled(false);
            mTvSubmit.setBackgroundResource(R.drawable.bg_click_false);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_submit:
                //开户申请
                OnEventUtils.onEvent(getActivity(), OnEventID.OPEN_ACCOUNT_SUBMIT);
                getTips();
                break;
            case R.id.tv_previous:
                mOnPreviousClickListener.onPreviousClick();
                break;
            case R.id.lay_occupation_type:
                if (dialog1 != null) {
                    dialog1.showDialog();
                }
                break;
            case R.id.lay_company_type:
                if (dialog2 != null) {
                    dialog2.showDialog();
                }
                break;
            case R.id.lay_position:
                if (dialog3 != null) {
                    dialog3.showDialog();
                }
                break;
            default:
                break;
        }
    }

    private void getTips() {
        showLoadingDialog();
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), new ReqTips(9), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                RespTips respTips = (RespTips) response;
                new ShowAgreementDialog(getActivity(), respTips, new ShowAgreementDialog.OnClickListener() {
                    @Override
                    public void onClick() {
                        LLApplication.reqUserRegister.setOpenSource(1);
                        EventBusUtils.post(new EventMessage(EventCode.EVENT_PRE_REGISTER));
                        EventBusUtils.post(new EventMessage(EventCode.EVENT_OPEN_ACCOUNT));
                        startActivity(new Intent(getActivity(), RegisterResultActivity.class));
                        getActivity().finish();
                    }
                }).show();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
            }
        });
    }

}
